package com.poly.police.monitor.admin.controller.sys;

import com.poly.police.monitor.admin.aop.Log;
import com.poly.police.monitor.application.service.sys.ISysRoleService;
import com.poly.police.monitor.common.aop.NotPower;
import com.poly.police.monitor.common.core.AjaxResult;
import com.poly.police.monitor.common.core.PageResult;
import com.poly.police.monitor.common.validator.annotation.IDMust;
import com.poly.police.monitor.pojo.validate.commons.IdValidate;
import com.poly.police.monitor.pojo.validate.commons.PageValidate;
import com.poly.police.monitor.pojo.validate.sys.SysRoleCreateValidate;
import com.poly.police.monitor.pojo.validate.sys.SysRoleUpdateValidate;
import com.poly.police.monitor.pojo.vo.sys.SysRoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("api/sys/role")
@Api(tags = "系统角色管理")
public class SysRoleController {

    @Resource
    ISysRoleService iSysRoleService;

    @NotPower
    @GetMapping("/all")
    @ApiOperation(value="所有角色")
    public AjaxResult<List<SysRoleVo>> all() {
        List<SysRoleVo> list = iSysRoleService.all();
        return AjaxResult.success(list);
    }

    @Log(title = "角色列表")
    @GetMapping("/list")
    @ApiOperation(value="角色列表")
    public AjaxResult<PageResult<SysRoleVo>> list(@Validated PageValidate pageValidate) {
        PageResult<SysRoleVo> list = iSysRoleService.list(pageValidate);
        return AjaxResult.success(list);
    }

    @Log(title = "角色详情")
    @GetMapping("/detail")
    @ApiOperation(value="角色详情")
    public AjaxResult<SysRoleVo> detail(@Validated @IDMust() @RequestParam("id") Long id) {
        SysRoleVo vo = iSysRoleService.detail(id);
        return AjaxResult.success(vo);
    }

    @Log(title = "角色新增")
    @PostMapping("/add")
    @ApiOperation(value="角色新增")
    public AjaxResult<Object> add(@Validated @RequestBody SysRoleCreateValidate createValidate) {
        iSysRoleService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "角色编辑")
    @PostMapping("/edit")
    @ApiOperation(value="角色编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody SysRoleUpdateValidate updateValidate) {
        iSysRoleService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "角色删除")
    @PostMapping("/del")
    @ApiOperation(value="角色删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iSysRoleService.del(idValidate.getId());
        return AjaxResult.success();
    }

}
